<?php

namespace Home\Controller;

class IndexController extends HomeController {
    
	protected $config = array('app_type' => 'public');
	//过滤查询字段

	public function index() {
	    
		$plugin['jquery-ui'] = true;
		$this -> assign("plugin", $plugin);

		//cookie("current_node", null);
		//cookie("top_menu", null);

		$config = D("UserConfig") -> get_config();
		$this -> assign("home_sort", $config['home_sort']);

		//$this -> _mail_list();
		$this -> _flow_list();
		//$this -> _schedule_list();
		$this -> _task_list();
        
		$this -> display();
	}
    
	
	public function set_sort() {
	    
		$val = I('val');
		$data['home_sort'] = $val;
		$model = D("UserConfig") -> set_config($data);
	}
	
	/**
	 * 生产任务
	 */
	public function _task_list() {

		$where_log['status'] = array('lt', 20);
		$where_log['executor'] = get_user_id();
		$where_log['type'] = array('eq', 1);
		$task_id_list = M("TaskLog") -> where($where_log) -> getField('task_id', true);
		if (empty($task_id_list)) {
			$where['_string'] = '1=2';  // 不存在的条件就有问题
		} else {
			$where['id'] = array('in', $task_id_list);
		}
		$Task_undo = M("task") -> where($where) ->order("create_time desc") ->field("id,task_no,name,create_time") ->select();
		
		$where_check['status'] = array('lt', 20);
		$where_check['check_user_id'] = array('eq',get_user_id());
		$where_check['is_audit'] = array('eq', 1);
		$Task_check = M("task") -> where($where_check) ->order("create_time desc") ->field("id,task_no,name,create_time") ->select();
		
		$this->assign("task_undo",$Task_undo);
		$this->assign("task_check",$Task_check);
	}
    
	/**
	 * 邮箱
	 */
	protected function _mail_list() {
	    
		$user_id = get_user_id();
		$model = D('Mail');

		//获取最新邮件
		$where['user_id'] = $user_id;
		$where['is_del'] = array('eq', '0');
		$where['folder'] = array( array('eq', 1), array('gt', 6), 'or');

		$new_mail_list = $model -> where($where) -> field("id,name,create_time") -> order("create_time desc") -> limit(8) -> select();
		$this -> assign('new_mail_list', $new_mail_list);

		//获取未读邮件
		$where['read'] = array('eq', '0');
		$unread_mail_list = $model -> where($where) -> field("id,name,create_time") -> order("create_time desc") -> limit(8) -> select();
		$this -> assign('unread_mail_list', $unread_mail_list);
	}

	/**
	 * 审批列表
	 */
	protected function _flow_list() {
		
		$user_id = get_user_id();
		$emp_no = get_emp_no();
		$flow = D('Flow');
		//带审批的列表
		$FlowLog = M("FlowLog");
		$where['emp_no'] = $emp_no;
		$where['is_del'] = 0;
		$where['_string'] = "result is null";
		$log_list = $FlowLog -> where($where) -> field('flow_id') -> select();
		$log_list = rotate($log_list);

		if (!empty($log_list)) {
			$map['id'] = array('in', $log_list['flow_id']);
		} else {
			$map['_string'] = '1=2';
		}
		$todo_flow_list = $flow -> where($map) -> field("id,name,create_time") -> limit(8) -> order("create_time desc") -> select();
		$this -> assign("todo_flow_list", $todo_flow_list);

		//已提交
		$map = array();
		$map['user_id'] = $user_id;
		$map['step'] = array('gt', 10);

		$submit_flow_list = $flow -> where($map) -> field("id,name,create_time") -> limit(8) -> order("create_time desc") -> select();

		$this -> assign("submit_flow_list", $submit_flow_list);
	}

	
    /**
     * 日程列表
     */
	protected function _schedule_list() {
	    
		$user_id = get_user_id();
		$schedule = M('Schedule');
		//获取最新邮件
		$start_date = date("Y-m-d");
		$where['user_id'] = $user_id;
		$where['is_del'] = array('eq', 0);
		$where['start_time'] = array('egt', $start_date);
		$schedule_list = $schedule -> where($where) -> order('start_time,priority desc') -> limit(8) -> select();
		$this -> assign("schedule_list", $schedule_list);

		$todo = M("Todo");
		$reason['user_id'] = $user_id;
		$reason['status'] = array("in", "1,2");
		$todo_list = $todo -> where($reason) -> order('priority desc,sort asc') -> limit(8) -> select();
		$this -> assign("todo_list", $todo_list);
	}
    
	private function _get_filter_data($data, $shop_no) {
	    
	    $res = 0;
		$shop_no = array_filter(explode(",", $shop_no));
		foreach ($data as $key => $val) {
			if (in_array($key, $shop_no)) {
				$res += $val;
			}
		}
		return $res;
	}
}
?>